package com.gking.centerSystem.controller;

import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.Sort;
import com.gking.centerSystem.service.Sort.SortService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sort")
public class SortController {

    @Autowired
    private SortService sortService;

    /**
     * 获取分类列表
     *
     * @return s
     */
    @RequiresPermissions("CS:manager:get:sort")
    @GetMapping("/getSortList")
    public Result<List<Sort>> getSortList() {
        return sortService.getSortList();
    }

    /**
     * 创建子分类
     *
     * @param sort 分类信息
     * @return s
     */
    @RequiresPermissions("CS:manager:set:sort")
    @PostMapping("/createSort")
    public Result<String> createSort(@RequestBody Sort sort) {

        String description = sort.getDescription();
        String parentId = sort.getParentId();
        Integer weight = sort.getWeight();

        return sortService.createSort(description, parentId, weight);
    }

    /**
     * 修改分类信息
     *
     * @param sort   分类信息
     * @param sortId 修改分类时可带上，创建时无需
     * @return z
     */
    @RequiresPermissions("CS:manager:set:sort")
    @PostMapping("/updateSort")
    public Result<String> updateSort(@RequestBody Sort sort, @RequestParam String sortId) {

        String description = sort.getDescription();
        Integer weight = sort.getWeight();

        return sortService.updateSort(description, sortId, weight);
    }

    /**
     * 批量删除分类
     *
     * @param sortIdList 分类id列表
     * @return s
     */
    @RequiresPermissions("CS:manager:set:sort")
    @PostMapping("/deleteSorts")
    public Result<String> deleteSort(@RequestBody List<String> sortIdList) {
        return sortService.deleteSort(sortIdList);
    }

}
